package cn.wjh.business.controller;

import cn.wjh.business.domain.Competition;
import cn.wjh.business.domain.Judge;
import cn.wjh.business.domain.Views;
import cn.wjh.business.domain.Works;
import cn.wjh.business.service.ICompetitionService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 比赛Controller
 *
 * @author jiraWu
 * @date 2023-10-17
 */
@RestController
@RequestMapping("/business/competition")
public class CompetitionController extends BaseController {
	@Resource
	private ICompetitionService competitionService;

	@Resource
	private ISysUserService userService;

	/**
	 * 查询比赛列表
	 */
//	@PreAuthorize("@ss.hasPermi('business:competition:list')")
	@GetMapping("/list")
	@Anonymous
	public TableDataInfo list(Competition competition) {
		startPage();
		List<Competition> list = competitionService.selectCompetitionList(competition);
		return getDataTable(list);
	}

	/**
	 * 获取我参加的比赛
	 * @param competition 比赛
	 */
	@GetMapping("/my")
	public TableDataInfo myJoinCompetition(Competition competition) {
		startPage();
		List<Competition> list = competitionService.myJoinCompetition(competition);
		return getDataTable(list);
	}

	@GetMapping("/keyValue")
	@Anonymous
	public AjaxResult listKeyValue() {
		AjaxResult result = success();
		result.put(AjaxResult.DATA_TAG,competitionService.listKeyValue());
		return result;
	}

	/**
	 * 导出比赛列表
	 */
//	@PreAuthorize("@ss.hasPermi('business:competition:export')")
	@Log(title = "比赛", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, Competition competition) {
		List<Competition> list = competitionService.selectCompetitionList(competition);
		ExcelUtil<Competition> util = new ExcelUtil<Competition>(Competition.class);
		util.exportExcel(response, list, "比赛数据");
	}

	/**
	 * 获取比赛详细信息
	 */
//	@PreAuthorize("@ss.hasPermi('business:competition:query')")
	@GetMapping(value = "/{competitionId}")
	@Anonymous
	public AjaxResult getInfo(@PathVariable("competitionId") Long competitionId) {
		return success(competitionService.selectCompetitionByCompetitionId(competitionId));
	}

	/**
	 * 新增比赛
	 */
	@PreAuthorize("@ss.hasPermi('business:competition:add')")
	@Log(title = "比赛", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody Competition competition) {
		return toAjax(competitionService.insertCompetition(competition));
	}

	/**
	 * 修改比赛
	 */
	@PreAuthorize("@ss.hasPermi('business:competition:edit')")
	@Log(title = "比赛", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody Competition competition) {
		return toAjax(competitionService.updateCompetition(competition));
	}

	/**
	 * 删除比赛
	 */
//	@PreAuthorize("@ss.hasPermi('business:competition:remove')")
	@Log(title = "比赛", businessType = BusinessType.DELETE)
	@DeleteMapping("/{competitionIds}")
	public AjaxResult remove(@PathVariable Long[] competitionIds) {
		return toAjax(competitionService.deleteCompetitionByCompetitionIds(competitionIds));
	}
	/*评委选择列表*/
	@GetMapping("/allJudge")
	public AjaxResult getAllJudge() {
		List<SysUser> users = userService.getAllJudge();
		if (users.isEmpty()){
			return error("没有评委可选择");
		}
		List<Judge> judgeList = users.stream().map(Judge::new).collect(Collectors.toList());
		return success(judgeList);
	}

	/**
	 * 获取我负责点评的比赛
	 * @return
	 */
	@GetMapping("/getMyJudgementCompetitionList")
	public TableDataInfo getMyJudgementCompetitionList(Competition competition){
		startPage();
		List<Competition> list = competitionService.getMyJudgementCompetitionList(competition);
		return getDataTable(list);
	}

}
